﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/12/17 4:52:46
 * ************************************************************/
namespace Mozlite.Web.UI.Themes.Wow.Controllers
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using System.Web.Mvc;
    using Mozlite.Common;

    /// <summary>
    /// 默认控制器。
    /// </summary>
    public class HomeController : CommonBaseController
    {
        public ActionResult LogsView()
        {
            return GetThreadsView(Categories.GetCategoryFromCache("updatelog"));
        }

        public ActionResult Donate()
        {
            return View();
        }

        public ActionResult Download()
        {
            return View();
        }

        public ActionResult RaidersView()
        {
            return GetThreadsView(Categories.GetCategoryFromCache("raiders"));
        }

        private Thread GetThread()
        {
            Thread thread;
            string key = RouteData.Get<string>("key");
            if (key == null)
                thread = Threads.GetThread(RouteData.Get<int>("id"));
            else
                thread = Threads.GetThread(key, true);
            return thread;
        }

        public ActionResult ThreadView()
        {
            var thread = GetThread();
            if (thread != null)
            {
                ViewBag.Title = thread.Title;
                int size;
                ViewBag.Posts = Threads.LoadPosts(PageIndex - 1, AppConfig.CommentPageSize, thread.ThreadID, null, out size);
                ViewBag.Pages = GetPages(size, AppConfig.CommentPageSize);
            }
            else
                ViewBag.Title = Local("ThreadView.ThreadNotFound");
            return View(thread);
        }

        public ActionResult ThreadsView()
        {
            Category category;
            string key = RouteData.Get<string>("key");
            if (key == null)
                category = Categories.GetCategoryFromCache(RouteData.Get<int>("id"));
            else
                category = Categories.GetCategoryFromCache(key);
            return GetThreadsView(category);
        }

        private ActionResult GetThreadsView(Category category)
        {
            int? categoryID = null;
            if (category == null)
            {
                categoryID = null;
                ViewBag.Title = Local("ThreadsView.PageTitle");
            }
            else
            {
                categoryID = category.CategoryID;
                ViewBag.Title = category.CategoryName;
            }
            int size;
            var threads = Threads.GetThreads(new ThreadQuery { IsDesc = true, OrderBy = "ThreadID", PageSize = 20, PageIndex = PageIndex - 1, IsApproved = true, CategoryID = categoryID }, out size);
            ViewBag.Pages = (int)Math.Ceiling(size * 1.0 / 20);
            return View(threads);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ThreadView(FormCollection form)
        {
            if (ModelState.IsValid)
            {
                var thread = GetThread();
                if (thread.AllowedApply)
                {
                    Post post = new Post();
                    post.Body = form.Get<string>("Body", "").RemoveHTML().Truncate(Permission.CommentLength);
                    if (string.IsNullOrEmpty(post.Body))
                        return RedirectToAction("ThreadView");
                    post.IsApproved = IsAuthenticated;
                    post.ThreadID = form.Get<int>("ThreadID");
                    Threads.SavePost(post);
                }
            }
            return RedirectToAction("ThreadView");
        }
    }
}